package com.js.baseview.splash;

// Created by JS on 2021/1/15.

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.OvershootInterpolator;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.js.baseview.R;

public class ParallaxRootLayout extends LinearLayout {

    private float angle = 0f;
    private float width = 0f;
    private float height = 0f;
    private Path path1, path2;
    private Paint paint;
    private ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);

    public ParallaxRootLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);

        path1 = new Path();
        path2 = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = (float) w;
        height = (float) h;

        Log.d("JS", "width:" + width + " ,  height:" + height);


        angle = (float) Math.atan(height * 0.2f / width);

        anim.setDuration(1200);
        anim.setInterpolator(new OvershootInterpolator(1.5f));
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float percent = (float) animation.getAnimatedValue();
                float r = 1.1f - percent;
                Log.d("JS", "width:" + width + " ,  height:" + height);
                path1.reset();
                path1.moveTo(width, 0f);
                path1.lineTo(width, height * 0.3f);
                path1.lineTo(width * r, (float) (height * 0.3f + Math.tan(angle) * width * percent));
                path1.lineTo(width * r, 0f);
                path1.close();

                path2.reset();
                path2.moveTo(0f, height * 0.7f);
                path2.lineTo(0f, height);
                path2.lineTo(width * 0.9f * percent, height);
                path2.lineTo(width * 0.9f * percent, (float) (height * 0.7f - Math.tan(angle) * width * 0.9f * percent));
                path2.close();

                invalidate();
            }
        });
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        anim.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (anim.isRunning()) {
            anim.end();
            anim.cancel();
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.drawColor(getResources().getColor(R.color.colorAccent));
        paint.setColor(getResources().getColor(R.color.colorPrimaryDark));
        canvas.drawPath(path1, paint);

        paint.setColor(getResources().getColor(R.color.colorPrimary));
        canvas.drawPath(path2, paint);
        super.dispatchDraw(canvas);
    }
}
